<?php
/*
 * 合同附件在线查看与下载接口
 */

require_once dirname(dirname(__FILE__)) . '/include/common.inc.php';
require_once INCLUDE_ROOT_PATH . 'file_server.class.php';

$shop_id = isset($_REQUEST['shop_id']) ? $_REQUEST['shop_id'] : '';    // 店铺ID
$act = isset($_REQUEST['act']) ? $_REQUEST['act'] : '';    //操作，upload上传，delete删除
$filename = isset($_REQUEST['filename']) ? $_REQUEST['filename'] : '';    //要操作的文件名
$token = isset($_REQUEST['token']) ? $_REQUEST['token'] : '';    //token验证

if (!isset($shop_id) || !is_numeric($shop_id) || $shop_id < 0 || ($act == 'view' && empty($filename)) || $token != md5($shop_id . 'basecontract' . $act . $filename)) {
    $result = false;
} else {
    $date_y = substr($filename, 0, 4);
    $date_m = substr($filename, 4, 2);
    $date_d = substr($filename, 6, 2);
    $file_dir = BASE_CONTRACT_ROOT_PATH . $date_y . '/' . $date_m . '/' . $date_d . '/' . $shop_id . '/';
    $filepath = $file_dir . $filename;
    $File_server = new File_server();
    switch ($act) {
        // 将PDF转为图片
        case 'pdfimgpath':
            $result = $File_server->pdf2jpg($filepath, $file_dir . "images/", $filename);
            break;
        // 将图片展示出来
        case 'viewimg':
            $imagepath = $file_dir . "images/" . $filename;
            $result = $File_server->view($imagepath);
            break;
        case 'view':
            $result = $File_server->view($filepath);
        case 'download':
            $result = $File_server->download($filepath);
            break;
        default:
            $result = false;
            break;
    }
}
exit;
